library(testthat)
context("avm test2")

test_that("avm test2", {
    #建立数据库连接
    con<-dbconnect()
    # 读取基础数据
    tabln.vec<-loadData(con=con,month_offset = -2)
    tabln.vec$ha_info.sp<-tabln.vec$ha_info.sp%>%remove.spatial.outlier(city_code='bj')
    # 建立poi相关信息
    poi.data.list<-getpoi.ras(con=con,tabln.vec = tabln.vec)
    # 取出得到POI信息的小区信息
    tabln.vec$ha_info.sp<-poi.data.list$ha_info.sp
    # 准备建模数据
    data.sets.all<-avm.data.train(tabln.vec,proptype = '11')
    # 分割训练集和测试集
    data.sets<-data.split(data =data.sets.all,train.rate = 0.75)
    # 建模, 模拟小区房价
    avm.models<-avm.fit(data.train = data.sets$train,dependentY = 'saleprice',ntree = 500,nround = 1000)
    # 测试集预测
    pred.test.data<-avm.pred(train.models=avm.models,newdata=data.sets$test,testing = T)
    # sapply(pred.test.data$pred%>%head,exp)
    pred.test.data$rmse
    pred.test.data$mae
    ######################################################################################
    pre<-sapply(pred.test.data$pred,exp)
    err<-sapply(X = pre[,-1]%>%data.frame,function(x){pre[,'y'] - x})
    rmse <- function(error,idx.sb=NULL)
    {
        if(!is.null(idx.sb)){
            error[idx.sb]<-NA
        }
        sqrt(mean(error^2,na.rm=T))
    }
    print(nrmse<-sapply(err%>%data.frame,rmse)/(max(pre[,1],na.rm=T)-min(pre[,1],na.rm=T)))
    ##################################################################################
    # rmse<-matrix(nrow = 50,ncol =6 )
    # for(i in 1:50){
    #     data.sets<-data.split(data =data.sets.all,train.rate = 0.75)
    #     avm.models<-avm.fit(data.train = data.sets$train,dependentY = 'saleprice',ntree = 500,nround = 1000)
    #     pred.test.data<-avm.pred(train.models=avm.models,newdata=data.sets$test,testing = T)
    #     # sapply(pred.test.data$pred%>%head,exp)
    #     rmse[i,]<-pred.test.data$rmse
    #     # pred.test.data$mae
    # }

    # 保存路径，默认为'./models.avm'
    model.path<-model.store.path()
    # 模型保存
    avm.model.store(city_code='bj',avm.models = avm.models,
                    yearmonth = '2017-7',proptype = '11',dependentY = 'rentprice')
    # 保存相关数据
    saveRDS(tabln.vec,file.path(model.path,paste('bj','2017-7','tabha.rds',sep='_')))
    saveRDS(poi.data.list,file.path(model.path,paste('bj','2017-7','poiha.rds',sep='_')))

    ##################################################################################################################
    # 模型读取
    model.path<-getOption('avm.model.store.path')
    avm.models.2<-avm.model.restore(city_code='bj',yearmonth = '2017-7',proptype = '11',
                                    dependentY = 'rentprice')
    # 读取数据
    tabln.vec <- readRDS(file.path(model.path,paste('bj','2017-7','tabha.rds',sep='_')))
    poi.data.list <- readRDS(file.path(model.path,paste('bj','2017-7','poiha.rds',sep='_')))
    # 生成未知预测数据,
    pred.xy<-avm.pred.data(train.models = avm.models.2,x=116.4411,
                           y=39.848,
                           tabln.vec = tabln.vec,poi.data.list = poi.data.list)
    # data.sets$train[1,]
    # 用读取的模型预测
    avm.pred(train.models=avm.models.2,newdata=pred.xy,remove.dup = T)
    avm.pred(train.models=avm.models.2,newdata=pred.xy,remove.dup = T,
             model.list =list(ols = T, rlm = T,
                              svm = T, rft = T, gwr1 = F, xgb = T) )
    # 关闭所有数据库连接
    killDbConnections()
})
